package com.ysxsoft.wdg.adapter.center_st;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.order.OnOrderClickListener;
import com.ysxsoft.wdg.adapter.order.ZyOrderInnerAdapter;
import com.ysxsoft.wdg.bean.ZyOrderBean;
import com.ysxsoft.wdg.bean.cart.ShopcartBean;
import com.ysxsoft.wdg.bean.response.StOrderResponse;
import com.ysxsoft.wdg.util.ViewUtils;

import java.util.ArrayList;
import java.util.List;


public class StOrderAdapter extends RecyclerView.Adapter<StOrderAdapter.MyHolder> {

    public void setOnOrderClickListener(OnOrderClickListener onOrderClickListener) {
        this.onOrderClickListener = onOrderClickListener;
    }

    private OnOrderClickListener onOrderClickListener;


    private List<StOrderResponse.DataBean> list;
    private Context context;

    public StOrderAdapter(List<StOrderResponse.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setData(List<StOrderResponse.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, null); //解决条目显示不全
        final MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        StOrderResponse.DataBean item = list.get(position);

//        List<StOrderResponse.DataBean.DetailBean> ll=new ArrayList<>();
//        StOrderResponse.DataBean dataBean=new StOrderResponse.DataBean();
//        dataBean.setName(item.getProductName());
//        dataBean.setPic(item.getPic());
//        dataBean.setPrice(item.getPrice());
//        dataBean.setNum(item.getNum());
//        ll.add(dataBean);

        holder.recyclerView.setAdapter(new StOrderInnerAdapter(item.getDetail(), context));

        holder.tvNo.setText(item.getName());

        switch (item.getStatus()){
            case 0:
                holder.tvStatus.setText("待付款");
                break;
            case 1:
                holder.tvStatus.setText("待使用");
                break;
            case 2:
                holder.tvStatus.setText("待评价");
                break;
            case 5:
                holder.tvStatus.setText("退款中");
                break;
            case 6:
                holder.tvStatus.setText("退款成功");
                break;
            case 7:
                holder.tvStatus.setText("退款失败");
                break;
        }



        holder.tvMoneyH.setText(context.getString(R.string.price_rmb, item.getAccount()));
        holder.tvNumH.setText("共" + item.getTotal_num() + "件商品   实付");

        TextView btnLeft = holder.btnLeft;
        TextView btnRight = holder.btnRight;
        String textLeft = "取消订单", textRight = "立即支付";

        int status = item.getStatus();
        switch (status) {//0未付
            case 0://待付款
                textLeft = "取消订单";
                textRight = "立即支付";
                break;
            case 1://待使用
                textLeft = "退款";
                textRight = "立即使用";
                break;
            case 2://待评价
                textLeft = "";
                textRight = "立即评价";
                break;
            case 5:
                textLeft="";
                textRight="查看详情";
                break;
        }

        btnLeft.setText(textLeft);

//        btnRight.setVisibility(status == 2 ? View.GONE : View.VISIBLE);

        holder.tvTui.setVisibility(View.GONE);
        btnLeft.setVisibility((status == 2) ? View.GONE : View.VISIBLE);

        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOrderClickListener.OnLeftClickListener(status, position);
            }
        });

        if("1".equals(item.getDiscuss())){
            btnRight.setVisibility(View.GONE);
            holder.tvStatus.setText("已评价");
        }else{
            btnRight.setVisibility(View.VISIBLE);
            btnRight.setText(textRight);
            btnRight.setOnClickListener(new View.OnClickListener() {//1提醒发货
                @Override
                public void onClick(View v) {
                    onOrderClickListener.OnRightClickListener(status, position);
                }
            });
        }

        if(status==5){
            btnLeft.setVisibility(View.GONE);
            btnRight.setVisibility(View.GONE);
        }

        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOrderClickListener.OnItemClickListener(position);
            }
        });

        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return holder.rlItem.onTouchEvent(event);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlItem;
        private RecyclerView recyclerView;
        private TextView tvNo, tvStatus, tvMoneyH, tvNumH, tvTui;
        private TextView btnLeft, btnRight;

        public MyHolder(View itemView) {
            super(itemView);


            rlItem = itemView.findViewById(R.id.rl_item);

            tvNo = itemView.findViewById(R.id.tv_no);
            tvStatus = itemView.findViewById(R.id.tv_status);


            recyclerView = itemView.findViewById(R.id.recyclerview);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setNestedScrollingEnabled(false);
            recyclerView.setFocusable(false);

            tvMoneyH = itemView.findViewById(R.id.tv_money_all);
            tvNumH = itemView.findViewById(R.id.tv_num_all);

            tvTui = itemView.findViewById(R.id.tv_tui);

            btnLeft = itemView.findViewById(R.id.tv_l);
            btnRight = itemView.findViewById(R.id.tv_r);
        }
    }
}
